<?php

class PacientesController extends AppController {

    var $name = "Pacientes";
    public $helpers = array('Js' => array('Jquery'), 'Crumb');
    public $components = array('RequestHandler');

    //var $components = array( 'RequestHandler' );

    function index() {
        $this->layout = "layout";
        //paginacion y ordenar
        $this->paginate = array('limit' => 2, 'page' => 1);
        $Paciente = $this->paginate('Paciente');
        $this->set(compact('Paciente'));
        //paginacion y ordenar
    }

    function agregar() {
        $this->layout = "layout";
        $this->set("accion", "Agregar");
        if ($this->request->is('post')) {
            //      var_dump($this->request->data);         // mirar el array que ese esta llenado para registrar
            if (isset($this->request->data['User']['password2']))
                $this->request->data['User']['password2hashed'] = $this->Auth->password($this->request->data['User']['password2']);
            $this->request->data = Sanitize::clean($this->request->data, array('encode' => false));
            //print_r($this->request->data);
            $this->Paciente->User->create();     //para crear un paciente ptimero se regustra un usuario  
            $password = $this->request->data["User"]["password"];
            $password2 = $this->request->data["User"]["password2"];
            $this->request->data["User"]["password"] = $this->Auth->password($this->request->data["User"]["password"]);
            $this->request->data["User"]["tipo"] = 1;
            if ($this->Paciente->saveAll($this->request->data)) {  // SaveAll guarda en todas las tablas involucradas
                $this->Session->setFlash('Paciente Registrado con exito.');
                $this->redirect(array('controller' => 'pacientes', 'action' => 'index'));
            } else {
                $this->request->data["User"]["password"] = $password;
                $this->request->data["User"]["password2"] = $password2;
                $this->Session->setFlash('No se pudo registrar el paciente..');
            }
        }
    }

    function ver($paciente_id = null) {
        $this->layout = "layout";
        $paciente = $this->Paciente->getInfoPacientebyId($paciente_id);
        $this->set('paciente', $paciente[0]);
    }

    function modificar($paciente_id = null) {
        $this->layout = "layout";
        if (empty($this->request->data)) {
            $paciente = $this->Paciente->getInfoPacientebyId($paciente_id);
            $this->request->data = $paciente[0];
            $this->request->data['User']['password'] = "";
        } else {
            $this->Paciente->User->create();     //para crear un paciente ptimero se regustra un usuario
            if ($this->Paciente->saveAll($this->request->data)) {  // SaveAll guarda en todas las tablas involucradas
                $this->Session->setFlash('Los datos del Paciente fueron modificado con exito.');
                $this->redirect(array('controller' => 'pacientes', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Los datos del Paciente no se pudieron modificar.');
            }
        }
        $this->set("accion", "Modificar");
    }

    function eliminar($paciente_id = null) {
        $this->layout = "layout";
        if ($this->Paciente->delete($paciente_id, $true))
            $this->Session->setFlash('Paciente eliminado con exito.');
        else
            $this->Session->setFlash('Paciente no se pudo eliminar.');
        $this->redirect(array('controller' => 'pacientes', 'action' => 'index'));
    }

}

?>